﻿using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

public class EnemyKingdom : Kingdom
{
	// constructors
	public EnemyKingdom() : this(Galaxy.CreateRandRace())
	{
	}

	public EnemyKingdom(Race race) : base(race)
	{
		this.FillDev = 2;							// enemy will fillup dev queue with 2 facilities instead of 1
	}

	// class methods
	public override void OnBeginTurn()
	{
		Debug.Log(Race.RaceType.ToString() + " Enemy Begin: " + Core.TotalTurn.ToString());

		base.OnBeginTurn();

		List<Fleet> ldock = new List<Fleet>();				// store fleets to be docked

		foreach (SolarPlanet sp in Planets)
			foreach (Fleet fle in sp.pSolar.Launching)
			{
				if (fle.Belong == this && MoveFleetLocation(fle))			// must detect Belong or else other kingdom's ships are launched
				{
					if (fle.Turn < 1)					// destination reached
						ldock.Add(fle);
				}
			}

		if (ldock.Count > 0)
			MovingFleets(ldock);

	}

	public override void OnEndTurn()
	{
		base.OnEndTurn();

		List<Planet> lplt = new List<Planet>();
		List<Planet> ladd = new List<Planet>();

		foreach (SolarPlanet sp in this.Planets)
		{
			if (sp.pSolar.SolarColony.ContainsKey(this as Kingdom) && (int)sp.pSolar.SolarColony[this as Kingdom] > 0)
			{
				foreach (Planet pl in sp.pSolar.Planets)
					if (!pl.Giant && pl.Status == Planet.PlanetStatus.None)
						lplt.Add(pl);

				if (lplt.Count < (int)sp.pSolar.SolarColony[this as Kingdom])
				{
					Debug.Log("Colony base overflow! Destroyed = " + ((int)sp.pSolar.SolarColony[this as Kingdom] - lplt.Count).ToString());
					sp.pSolar.SolarColony[this as Kingdom] = lplt.Count - (int)sp.pSolar.SolarColony[this as Kingdom];
				}

				if (lplt.Count > 0)
				{
					int count = (int)sp.pSolar.SolarColony[this as Kingdom];

					for (int i = 0; i < count; i++)
					{
						int rand = Utility.Random(lplt.Count);
						ladd.Add(lplt[rand]);
						lplt.RemoveAt(rand);
					}
				}

			}
		}

		// to add new solarplanet here for avoiding iteration conflict on this.Planets
		foreach (Planet plt in ladd)
		{
			RegisterNewColony(this as Kingdom, plt, true);
			plt.AutoMode = true;			// set planet auto mode for enemy kingdoms
		}

		// to send colony ships and outpost ships
		List<Fleet> lship = new List<Fleet>();
		bool tosend = false;
		foreach (Fleet fleet in this.Fleets)
		{
			if (fleet is ColonyShip || fleet is OutpostShip)
				lship.Add(fleet);

			if ((fleet is ColonyShip && !fleet.Launched) || (fleet is OutpostShip && !fleet.Launched))
				tosend = true;
		}

		//foreach (Fleet f in lship)
		//  Debug.Log(Race.RaceType.ToString() + " #" + f.FleetID.ToString() + f.DisplayName 
		//    + f.Launched.ToString() + "|" + tosend.ToString());

		if (tosend)
			SendColonyShip();

		Debug.Log(Race.RaceType.ToString() + " Enemy End: " + Core.TotalTurn.ToString());
	}

	public override bool MovingFleets(List<Fleet> lflts)
	{
		// to handle docking ships
		foreach (Fleet fl in lflts)
		{
			bool destroy = false;
			DockingNewSolar(fl, fl.Dest, out destroy);

			if (destroy)					// to destroy used outpost
				DestroyFleet(fl);

			if (fl is ColonyShip && fl.Home.OwnedBy.Contains(this) && !IsPlanetOwned(fl.Home))		// new home updated in "DockingNewSolar()
			{
				List<Planet> lplt = new List<Planet>();

				foreach (Planet pl in fl.Home.Planets)
					if (pl.Status == Planet.PlanetStatus.None)
						lplt.Add(pl);

				Planet plt = lplt[Utility.Random(lplt.Count)];
				RegisterNewColony(this as Kingdom, plt, false);
				plt.AutoMode = true;

				DestroyFleet(fl);		// to destroy used colony ship
			}
		}

		return base.MovingFleets(lflts);;
	}
}